// Copyright 2022 jeff.li. and/or its affiliates.


#include <tbir/runtime/container.h>
#include <tbir/runtime/container_private.h>
#include <tbir/runtime/ft_container.h>
#include <tbir/runtime/registry.h>

namespace tbir::runtime {

    TBIR_REGISTER_GLOBAL("runtime.Regex")
    .set_body_typed([](unicode_view pattern,
                       bool ignore_case,
                       bool dotall,
                       bool extended,
                       bool anchored,
                       bool ucp) {
        return Regex(pattern, ignore_case, dotall, extended, anchored, ucp);
    });

    TBIR_REGISTER_GLOBAL("runtime.RegexSplit").set_body_typed([](Regex regex_ref, RTView string) {
        return regex_ref.split(string);
    });

    TBIR_REGISTER_GLOBAL("runtime.RegexReplace").set_body([](PyArgs args) -> RTValue {
        MXCHECK(args.size() == 3) << "runtime.RegexReplace expected 3 arguments but got " << args.size();
        Regex regex_ref = args[0].As<Regex>();
        return regex_ref.replace(args[1], args[2]);
    });

    TBIR_REGISTER_GLOBAL("runtime.RegexMatch").set_body([](PyArgs args) -> RTValue {
        MXCHECK(args.size() == 2 || args.size() == 3)
            << "runtime.RegexMatch expected 2 or 3 arguments but got " << args.size();
        Regex regex_ref = args[0].As<Regex>();
        if (args.size() == 2) {
            return regex_ref.match(args[1], 0);
        } else {
            return regex_ref.match(args[1], args[2].As<int64_t>());
        }
    });

}  // namespace tbir::runtime
